package com.jokui.rao.auth.ali_auth;

import android.content.Context;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.WindowManager;

public class AppUtils {
    public static int dp2px(Context context, float dipValue) {
        try {
            final float scale = context.getResources().getDisplayMetrics().density;
            return (int) (dipValue * scale + 0.5f);
        } catch (Exception e) {
            return (int) dipValue;
        }
    }

    public static int px2dp2(Context context, float dipValue) {
        float scale1 = (float) getPhoneHeightPixels(context) / 1080;
        Log.e("xxxxxx", getPhoneHeightPixels(context) + "|<><>|" + scale1);
        return (int) (dipValue * scale1);
    }
    public static int px2dp2y(Context context, float dipValue) {
        return Math.max(AppUtils.px2dp2(context, dipValue) - 90, 0);
    }

    public static int px2dp(Context context, float px) {
        float ddd = 1;
//        ddd = (float)getPhoneWidthPixels(context) / (float)getPhoneHeightPixels(context) / ((float)2208/(float)1242);
        try {
            float scale = context.getResources().getDisplayMetrics().density;
            float relScale = context.getResources().getDisplayMetrics().heightPixels / context.getResources().getDisplayMetrics().ydpi;
            scale = RomUtil.isEmui() ?  Math.max(scale, relScale) : scale;
            return (int) (px / scale * ddd + 0.5f);
        } catch (Exception e) {
            return (int) px;
        }
    }

    public static int getPhoneWidthPixels(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics var2 = new DisplayMetrics();
        if (wm != null) {
            wm.getDefaultDisplay().getMetrics(var2);
        }

        return var2.widthPixels;
    }

    public static int getPhoneHeightPixels(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics var2 = new DisplayMetrics();
        if (wm != null) {
            wm.getDefaultDisplay().getMetrics(var2);
        }

        return var2.heightPixels;
    }
}
